import { LevelVo } from "./SimpleVo";
import { LevelConfig } from "../config/LevelConfig";
import CommonSaveData from "../../LTGame/Commom/CommonSaveData";

/**
 * 关卡数据处理类  (其他有需求表格可以仿照这种写法)
 */
export default class LevelProxy {
    public levelVos: { [key: string]: LevelVo } = {};
    public level_data: LevelConfig.config[][] = [];

    public static get instance(): LevelProxy {
        if (this._instance == null) {
            this._instance = new LevelProxy();
        }
        return this._instance;
    }
    private static _instance: LevelProxy;

    private constructor() {
        this.initData();
    }

    initData() {
        LevelConfig.dataList.forEach((lvConfig: LevelConfig.config) => {
            this.level_data[lvConfig.lv] = this.level_data[lvConfig.lv] || [];
            this.level_data[lvConfig.lv].push(lvConfig);
        })
    }

    levelVoByLv(lv: number, smallLv: number): LevelVo {
        let config = this.getLevelConfigByLv(lv, smallLv);
        if (!config) {
            return null;
        }
        return this.levelVos[lv + "_" + smallLv] ||
            (this.levelVos[lv + "_" + smallLv] = new LevelVo(config.id));
    }

    levelVoById(id: number): LevelVo {
        let config = LevelConfig.data[id];
        return config ? this.levelVoByLv(config.lv, config.smallLv) : null;
    }

    getLevelConfigByLv(level: number, minilevel: number) {
        return this.level_data[level][minilevel - 1];
    }

    /** 获取大关卡配置 */
    get_level_datas(level: number): LevelConfig.config[] {
        level = level || 1;
        return this.level_data[level];
    }

    nowLevelVo(): LevelVo {
        let config = LevelConfig.data[CommonSaveData.instance.levelId];
        return this.levelVoByLv(config.lv, config.smallLv);
    }

    nextLevelVo(): LevelVo {
        let nextId = CommonSaveData.instance.levelId + 1;
        let config = LevelConfig.data[nextId];
        if (!config) {
            // 从第一关开始
            return this.levelVoByLv(1, 1);
        }
        return this.levelVoById(nextId);
    }

    nowLevelId(): number {
        return CommonSaveData.instance.levelId;
    }

    nextLevelId(): number {
        let vo = this.nextLevelVo();
        return vo.levelData.id;
    }
}